package com.akaovi.exp.exp10;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Two extends Application {
    // 字体
    Font title = new Font(16);
    Font text = new Font(14);
    GridPane root = new GridPane();
    int year = 2022;
    int month = 10;  // 11月
    Label label = new Label(String.format(" %d年%d月 ", this.year, this.month + 1));
    Button turnLeft = new Button("<<");
    Button turnRight = new Button(">>");

    Label seven = new Label("日");  // 0列
    Label one = new Label("一");  // 1列
    Label two = new Label("二");  // 2列
    Label three = new Label("三");  // 3列
    Label four = new Label("四");  // 4列
    Label five = new Label("五");  // 5列
    Label six = new Label("六");  // 6列

    String[] dayOfWeek = new String[]{"", "seven", "one", "two", "three", "four", "five", "six"};

    @Override
    public void start(Stage stage) {
        setWidth(root);
        setHeight(root);
        setHeader(root);
        setWeekLabel(root);

        initDays(root);
        setDaysInForm();

        // 给按钮绑定事件
        setEventForTurnRight();
        setEventForTurnLeft();

        // 调试: 开启网格
//        root.setGridLinesVisible(true);
        Scene scene = new Scene(root, 420, 320);
        stage.setTitle("日历表");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

    private void setWidth(GridPane gridPane) {
        ColumnConstraints columnConstraints = new ColumnConstraints(60);
        columnConstraints.setHalignment(HPos.CENTER);
        gridPane.getColumnConstraints().add(columnConstraints);
        gridPane.getColumnConstraints().add(columnConstraints);
        gridPane.getColumnConstraints().add(columnConstraints);
        gridPane.getColumnConstraints().add(columnConstraints);
        gridPane.getColumnConstraints().add(columnConstraints);
        gridPane.getColumnConstraints().add(columnConstraints);
        gridPane.getColumnConstraints().add(columnConstraints);
    }

    private void setHeight(GridPane gridPane) {
        RowConstraints rowConstraints = new RowConstraints(40);
        rowConstraints.setValignment(VPos.CENTER);
        gridPane.getRowConstraints().add(rowConstraints);
        gridPane.getRowConstraints().add(rowConstraints);
        gridPane.getRowConstraints().add(rowConstraints);
        gridPane.getRowConstraints().add(rowConstraints);
        gridPane.getRowConstraints().add(rowConstraints);
        gridPane.getRowConstraints().add(rowConstraints);
        gridPane.getRowConstraints().add(rowConstraints);
        gridPane.getRowConstraints().add(rowConstraints);
    }

    private void setHeader(GridPane root) {
        label.setAlignment(Pos.CENTER);
        label.setFont(title);
        turnLeft.setAlignment(Pos.CENTER);
        turnRight.setAlignment(Pos.CENTER);
        root.add(label, 2, 0, 3, 1);
        root.add(turnLeft, 0, 0, 1, 1);
        root.add(turnRight, 6, 0, 1, 1);
    }

    private void setWeekLabel(GridPane root) {
        seven.setAlignment(Pos.CENTER);
        seven.setFont(text);
        one.setAlignment(Pos.CENTER);
        one.setFont(text);
        two.setAlignment(Pos.CENTER);
        two.setFont(text);
        three.setAlignment(Pos.CENTER);
        three.setFont(text);
        four.setAlignment(Pos.CENTER);
        four.setFont(text);
        five.setAlignment(Pos.CENTER);
        five.setFont(text);
        six.setAlignment(Pos.CENTER);
        six.setFont(text);
        root.add(seven, 0, 1);
        root.add(one, 1, 1);
        root.add(two, 2, 1);
        root.add(three, 3, 1);
        root.add(four, 4, 1);
        root.add(five, 5, 1);
        root.add(six, 6, 1);
    }

    private void setDaysInForm() {
        cleanDays(root);
        GregorianCalendar gregorianCalendar = new GregorianCalendar(year, month, 1);
        int maxDays = gregorianCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        String w = dayOfWeek[gregorianCalendar.get(Calendar.DAY_OF_WEEK)];
        int startIdx = -1;
        if ("seven".equals(w)) {
            startIdx = 10;
        } else if ("one".equals(w)) {
            startIdx = 11;
        } else if ("two".equals(w)) {
            startIdx = 12;
        } else if ("three".equals(w)) {
            startIdx = 13;
        } else if ("four".equals(w)) {
            startIdx = 14;
        } else if ("five".equals(w)) {
            startIdx = 15;
        } else if ("six".equals(w)) {
            startIdx = 16;
        }
        for (int i = startIdx; i < maxDays + startIdx; ++i) {
            ((Label) root.getChildren().get(i)).setText(String.format("%d", i - startIdx + 1));
        }
    }

    // 清理标签
    private void cleanDays(GridPane root) {
        // 2-0 的下标为 10  最后一个的为 51
        for (int i = 10; i < 52; ++i) {
            ((Label) root.getChildren().get(i)).setText("");
        }
    }

    // 初始化组件
    private void initDays(GridPane root) {
        for (int i = 2; i < 8; ++i) {
            for (int j = 0; j < 7; ++j) {
                Label l = new Label("");
                l.setFont(text);
                l.setAlignment(Pos.CENTER);
                root.add(l, j, i);
            }
        }
    }

    private void setEventForTurnRight() {
        turnRight.setOnAction((event) -> {
            month = (month + 1) % 12;
            if (month == 0) {
                year++;
            }
            setDaysInForm();
            label.setText(String.format(" %d年%d月 ", this.year, this.month + 1));
        });
    }

    private void setEventForTurnLeft() {
        turnLeft.setOnAction((event) -> {
            month = (month + 12 - 1) % 12;
            if (month == 11) {
                year--;
            }
            setDaysInForm();
            label.setText(String.format(" %d年%d月 ", this.year, this.month + 1));
        });
    }
}